<?php
class Index_SettingAction extends C_Action  
{
	private $user_E = null;
	private $site_E = null;
	private $current_room_E = null;
	private $RootOper = 0;
	public function init()
	{
		//持有site 拦截器和用户拦截器
		//当具有RootOper 权限的时候才能执行
		
		$this->user_E = Zend_Registry::get("user_E");
		$this->site_E = Zend_Registry::get("site_E");
		$this->current_room_E = Zend_Registry::get("current_room_E");
		$this->RootOper = Zend_Registry::get('RootOper');
		if($this->RootOper == 0)
		{
			$data['error'] = '内部错误！请联系网站客服！'; 	echo json_encode($data); exit; 
			exit;
		}
	}
	public function doRoomrename()
	{
		$name = $this->_request->get('name');
		if(empty($name))
		{
			$data['error'] = '房间名字不能为空！'; 	echo json_encode($data); exit; 
			exit;
		}
		$this->current_room_E->name = $name;
		
	}
	public function doWidget()
	{
		
		$widgetid = $this->_request->get('kind');
		if(empty($widgetid))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$widget_E = Widgets::getById($widgetid);
		if(empty($widget_E))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		
		$mod_num = Mods::getNum($this->current_room_E->id);
		if($mod_num >=6)
		{
			$data['error'] = '一个房间最多6个组件！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_type = $widget_E->shortname;
		
		
		
		$info['siteid'] = $this->site_E->id;
		$info['roomid'] = $this->current_room_E->id;
		$info['userid'] = $this->user_E->id;
		$info['widgetid'] = $widgetid;
		$info['widgetname'] = $mod_type;
		$mod_E = Mods::insertEntity($info);
		
		/////////////////////////////////////////////////
		$modsort = $this->current_room_E->modsort;
		$modsort = unserialize($modsort);
		if(is_array($modsort))
		{
			if(!empty($modsort['l']))
			$modsort['l'] = $mod_E->id.','.$modsort['l'];
			else 
			$modsort['l'] = $mod_E->id.",";
		}
		else 
		{
			$modsort['l'] = $mod_E->id.",";
			$modsort['r'] = '';
		}
		$this->current_room_E->modsort = serialize($modsort);
		/////////////////////////////////////////////////////////
		
		
		
		$mod_obj = $this->getmodObj($mod_E);
		$data = $mod_obj->showTpl();
		////////////////////////////////////////////////////////////
		
		
		$mod_E->title = $this->current_room_E->name.$mod_obj->getName();
		
		
		echo json_encode($data);
	}
	
	public function doSetting()
	{
		$modid = $this->_request->get('modid');
		if(empty($modid))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_E = Mods::getById($modid);
		if(empty($mod_E))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_obj = $this->getmodObj($mod_E);
		$data = $mod_obj->showSettingTpl();
		
		echo json_encode($data);
	}
	
	
	public function doModsetting()
	{
		$modid = $this->_request->get('modid');
		if(empty($modid))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_E = Mods::getById($modid);
		if(empty($mod_E))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		
		$mod_obj = $this->getmodObj($mod_E);
		$data = $mod_obj->modsetting($mod_E,$this->_request);
		echo json_encode($data);
	}
	public function doModdel()
	{
		$modid = $this->_request->get('modid');
		if(empty($modid))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_E = Mods::getById($modid);
		if(empty($mod_E))
		{
			$data['error'] = '错误！'; 	echo json_encode($data); exit; 
			exit;
		}
		$mod_obj = $this->getmodObj($mod_E);
		$data = $mod_obj->moddel($mod_E, $this->current_room_E);
		echo json_encode($data);
	}
	public function doLayout()
	{
		$mods = $this->_request->get("mods");
		$r_mods = $this->_request->get("r_mods");
		
		if(empty($mods) && empty($r_mods))
		{
			$data['error'] = 1;
			echo json_encode($data);
			exit;
		}
		if(!empty($mods))
		{
			$modsort['l'] = $mods.",";
		}
		if(!empty($r_mods))
		{
			$modsort['r'] = $r_mods.",";
		}
		$this->current_room_E->modsort = serialize($modsort);
		$data['error'] = 0;
		echo json_encode($data);
	}
	public function doDelroom()
	{
		$room_num = Rooms::getNum($this->site_E->id);
		if($room_num <=1)
		{
			$data['error'] = '最后一个房间了，如果删掉你系统会蓝屏啊，有木有！';
			echo json_encode($data);
			return;
		}
		$mod_num = Mods::getNum($this->current_room_E->id);
		if($mod_num > 0)
		{
			$data['error'] = '需要先把空间里的应用模块清理掉 再删除房间！';
			echo json_encode($data);
			return;
		}
		$this->current_room_E->delete();
		$data['r'] = 0;
		echo json_encode($data);
	}
	private function getmodObj($mod_E)
	{
		$mod_type = $mod_E->widgetname;
		$mod_class = ucwords($mod_type);
		$mod_obj = new $mod_class();
		
		if(!$mod_obj instanceof Sitebase) {$data['error'] = '内部错误！请联系网站客服！'; 	echo json_encode($data); exit; }
		
		$mod_param['user_E'] = $this->user_E;
		$mod_param['site_E'] = $this->site_E;
		$mod_param['current_room_E'] = $this->current_room_E;
		$mod_param['mod_E'] = $mod_E;
		$mod_obj->setParam($mod_param, $this);
		return $mod_obj;
	}
}
?>